package thread;

/**
 * 互斥性
 * 当使用多个synchronized锁定多个不同的代码片段，并且指定的同步监视器对象
 * 相同时，这些代码片段之间就是互斥的，多个线程不能同时操作他们
 */
public class SyncDemo6 {
    public static void main(String[] args) {
        Boo1 boo1 = new Boo1();
        Thread t1 = new Thread(boo1::methodsA);
        Thread t2 = new Thread(boo1::methodsB);
        t1.start();
        t2.start();
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
    }
}

class Boo1 {
    public synchronized void methodsA(){

        Thread t = Thread.currentThread();
        System.out.println(t.getName()+"方法A开始执行");
        try {
            Thread.sleep(1000);
        }catch (InterruptedException e){

        }
        System.out.println(t.getName()+"方法A执行完毕");
    }

    public void methodsB(){
        synchronized (this){
            Thread t = Thread.currentThread();
            System.out.println(t.getName()+"方法B开始执行");
            try {
                Thread.sleep(1000);
            }catch (InterruptedException e){

            }
            System.out.println(t.getName()+"方法B执行完毕");
        }

    }
}